﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConquerServer_Basic
{
    public class NpcProcessor
    {
        public static int dialog(GameClient Client, string[] dlg)
        {
            NpcReplyPacket Reply = new NpcReplyPacket();

            foreach (string parse_dlg in dlg)
            {
                if (parse_dlg.StartsWith("AVATAR"))
                {
                    string str_avatar = parse_dlg.Substring(7, parse_dlg.Length - 7);
                    Reply.Reset();
                    Reply.InteractType = NpcReplyPacket.Avatar;
                    Reply.wParam = ushort.Parse(str_avatar);
                    Client.Send(Reply);
                }
                else if (parse_dlg.StartsWith("TEXT"))
                {
                    string str_text = parse_dlg.Substring(5, parse_dlg.Length - 5);
                    Reply.Reset();
                    Reply.InteractType = NpcReplyPacket.Dialog;
                    Reply.Text = str_text;
                    Client.Send(Reply);
                }
                else if (parse_dlg.StartsWith("OPTION"))
                {
                    string str_op_num = parse_dlg.Substring(6, parse_dlg.IndexOf(' ') - 6);
                    string str_op_text = parse_dlg.Substring(6 + str_op_num.Length + 1, parse_dlg.Length - 6 - str_op_num.Length - 1);
                    Reply.Reset();
                    Reply.InteractType = NpcReplyPacket.Option;
                    Reply.OptionID = (byte)short.Parse(str_op_num);
                    Reply.Text = str_op_text;
                    Client.Send(Reply);
                }
                else if (parse_dlg.StartsWith("INPUT"))
                {
                    string str_op_num = parse_dlg.Substring(5, parse_dlg.IndexOf(' ') - 5);
                    string str_txt_len = parse_dlg.Substring(5 + str_op_num.Length + 1, parse_dlg.Length - 5 - str_op_num.Length - 1);
                    Reply.Reset();
                    Reply.InteractType = NpcReplyPacket.Input;
                    Reply.wParam = ushort.Parse(str_txt_len);
                    Reply.OptionID = (byte)sbyte.Parse(str_op_num);
                    Client.Send(Reply);
                }
                else if (parse_dlg.StartsWith("NOP"))
                    continue;
                else
                    throw new ArgumentException("Failed to parse npc dialog statement `" + parse_dlg + "`");
            }
            Reply.Reset();
            Reply.InteractType = NpcReplyPacket.Finish;
            Reply.DontDisplay = false;
            Client.Send(Reply);
            return 0;
        }

        public static void Process(GameClient Client, byte OptionID, string Input)
        {
            switch (Client.ActiveNpcID)
            {
                default:
                    {
                        dialog(Client, new string[] {
                            "AVATAR 30",
                            "TEXT Sorry, this NPC is not implemented yet!",
                            "OPTION-1 Oops"
                        });
                        break;
                    }
            }
        }
    }
}
